import copy
from config import config


class Pagination(object):
    """
    自定义分页
    """
    def __init__(self, current_page, total_count, base_url, params, per_page_count=config.FLASKY_POSTS_PER_PAGE,
                 max_pager_count=11):
        try:
            current_page = int(current_page)
        except Exception as e:
            current_page = 1

        if current_page <= 0:
            current_page = 1

        self.current_page = current_page
        # 数据总条数
        self.total_count = total_count
        # 每页显示条数
        self.per_page_count = per_page_count
        # 页面上显示的最大页码数
        max_page_num, div = divmod(total_count, per_page_count)
        if div:
            max_page_num += 1
        self.max_page_num = max_page_num
        # 页面默认显示11个页面（当前页在中间）
        self.max_pager_count = max_pager_count
        self.half_max_pager_count = int((max_pager_count - 1) / 2)
        # url前缀
        self.base_url = base_url
        # request.GET
        params = copy.deepcopy(params)
        get_dict = params.to_dict()
        self.params = get_dict

    @property
    def start(self):
        return (self.current_page - 1) * self.per_page_count

    @property
    def end(self):
        return self.current_page * self.per_page_count

    def page_html(self):
        # 如果总页数 <= 11
        if self.max_page_num <= self.max_pager_count:
            pager_start = 1
            pager_end = self.max_page_num

        # 总页数 > 11
        else:
            # 当前页 <= 5
            if self.current_page <= self.half_max_pager_count:
                pager_start = 1
                pager_end = self.max_pager_count
            else:
                # 当前页 + 5 > 总页面
                if (self.current_page + self.half_max_pager_count) > self.max_page_num:
                    pager_end = self.max_page_num
                    pager_start = self.max_page_num - self.max_pager_count + 1
                else:
                    pager_start = self.current_page - self.half_max_pager_count
                    pager_end = self.current_page + self.half_max_pager_count

        page_html_list = []

        page_num = 1
        first_page = '<li><a href="{}?p={}">首页</a></li>'.format(self.base_url, page_num)
        page_html_list.append(first_page)
        # last
        page_num = self.current_page - 1
        if page_num < 1:
            pervious_page = '<li class="disabled"><a href="{}?p={}" aria-label="Previous">上一页</span>' \
                            '</a></li>'.format(self.base_url, page_num)
        else:
            pervious_page = '<li><a href = "{}?p={}" aria-label = "Previous" >上一页</span></a></li>'.\
                format(self.base_url, page_num)
        page_html_list.append(pervious_page)

        # middle
        for i in range(pager_start, pager_end + 1):
            page_num = i
            if i == self.current_page:
                middle_page = '<li class="active"><a href="{}?p={}">{}</a></li>'.\
                    format(self.base_url, page_num, page_num)
            else:
                middle_page = '<li><a href="{}?p={}">{}</a></li>'.format(self.base_url, page_num, page_num)
            page_html_list.append(middle_page)
        # next
        page_num = self.current_page + 1
        if page_num > self.max_page_num:
            page_num = self.current_page
            next_page = '<li class="disabled"><a href = "{}?p={}" aria-label = "Next">下一页' \
                        '</span></a></li >'.format(self.base_url, page_num)
        else:
            next_page = '<li><a href = "{}?p={}" aria-label = "Next">下一页</span></a></li>'.\
                format(self.base_url, page_num)
        page_html_list.append(next_page)

        # 最后一页
        page_num = self.max_page_num
        last_page = '<li><a href="{}?p={}">尾页</a></li>'.format(self.base_url, page_html_list)
        page_html_list.append(last_page)

        return ''.join(page_html_list)
